package com.jintian.smart.kernel.core.jackson;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.BeanSerializer;
import com.fasterxml.jackson.databind.ser.std.BeanSerializerBase;
import com.fasterxml.jackson.databind.ser.std.MapSerializer;
import com.fasterxml.jackson.databind.ser.std.StdKeySerializers.StringKeySerializer;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.jintian.smart.kernel.core.domain.ITailEntity;

import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;

public class TailEntitySerializerExt extends BeanSerializer {
    /**
     *
     */
    private static final long serialVersionUID = 1L;

    MapSerializer ms = MapSerializer.construct(new HashSet<>(), null,
            TypeFactory.defaultInstance().constructMapType(HashMap.class, String.class, Object.class), false, null,
            null, null, null).withResolved(null, new StringKeySerializer(), null, null, null, false);

    public TailEntitySerializerExt(BeanSerializerBase src) {
        super(src);
    }

    @Override
    protected void serializeFields(Object bean, JsonGenerator gen, SerializerProvider provider) throws IOException {
        ms.serializeWithoutTypeInfo(((ITailEntity) bean).extAttrs(), gen, provider);
        super.serializeFields(bean, gen, provider);
    }

    @Override
    protected void serializeFieldsFiltered(Object bean, JsonGenerator gen, SerializerProvider provider)
            throws IOException {
        if (bean instanceof ITailEntity) {
            ms.serializeWithoutTypeInfo(((ITailEntity) bean).extAttrs(), gen, provider);
        }
        super.serializeFieldsFiltered(bean, gen, provider);
    }
}
